// pages/product/product.js 
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navIndex:0,
    nav:[
    ],
    product: [
    ],
    tabbarIndex: 1,
    tabbar: [
      {
        img: 'https://crm.bxwhl.com/static/tabbar/index.png',
        imgSelect: 'https://crm.bxwhl.com/static/tabbar/index-select.png',
        text: '名片',
        url: '/pages/index/index',
      },
      {
        img: 'https://crm.bxwhl.com/static/tabbar/goods.png',
        imgSelect: 'https://crm.bxwhl.com/static/tabbar/goods-select.png',
        text: '产品',
        url: '/pages/product/product',
      },
      {
        img: 'https://crm.bxwhl.com/static/tabbar/dynamic.png',
        imgSelect: 'https://crm.bxwhl.com/static/tabbar/dynamic-select.png',
        text: '动态',
        url: '/pages/dynamic/dynamic',
      },
      {
        img: 'https://crm.bxwhl.com/static/tabbar/website.png',
        imgSelect: 'https://crm.bxwhl.com/static/tabbar/website-select.png',
        text: '官网',
        url: '/pages/website/website',
      },
       {
        img: 'https://crm.bxwhl.com/static/tabbar/mine.png',
        imgSelect: 'https://crm.bxwhl.com/static/tabbar/mine-select.png',
        text: '我',
        url: '/pages/mine/mine',
      },
    ],
    
    // 消息条数
    noteNum: 0,
    readPercent: 0,
    actionSheetHidden: true,
    topbg:'',
  },
  navChoose:function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    var cateid = e.currentTarget.dataset.id;
    that.setData({
      navIndex:index,
    })
    app.httpRequest('goods/lists', {category_id: cateid}, 'get', false, function(res){
      that.setData({
        enterprise: res.data.enterprise,
        category: res.data.category,
        product: res.data.product,
        pageShow: true,
      })
    })
    
  },
  // 留言
  tochat: function () {
    app.confirmLogin(function(){
      wx.navigateTo({
        url: '/pages/chat/chat',
      })
    })
  },
  // 点击分享
  actionSheetTap: function () {
    var that = this;
    that.setData({
      actionSheetHidden: !that.data.actionSheetHidden
    })
  },
  actionSheetbindchange: function () {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  bindMenu1: function () {
    this.setData({
      menu: 1,
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  bindMenu2: function (e) {
    this.setData({
      menu: 2,
      actionSheetHidden: !this.data.actionSheetHidden
    })
    wx.navigateTo({
      url: '/pages/shareCard/shareCard',
    })
  },
  // 滚动计算阅读
  scroll: function (e) {
    var that = this;
    var scrollHeight = e.detail.scrollHeight;
    var scrollTop = e.detail.scrollTop;
    var windowHeight = that.data.windowHeight;
    var height = scrollHeight - windowHeight - 20;
    var scrollPercent = (scrollTop / height) * 100;
    scrollPercent = Math.ceil(scrollPercent);
    var readPercent = that.data.readPercent;
    if (scrollPercent > 100) {
      scrollPercent = 100;
      readPercent = 100;
    } else {
      scrollPercent = scrollPercent;
      if (scrollPercent > readPercent) {
        readPercent = scrollPercent;
      } else {
        readPercent = readPercent;
      }
    }
    that.setData({
      readPercent: readPercent
    })
  },
  toproductDetail:function(e){
    var that = this;
    var id = e.currentTarget.id;
    wx.navigateTo({
      url: '/pages/productDetail/productDetail?id='+id,
    })
  },
  getPhoneNumber: function (e) {
    var that = this;
    var iv = e.detail.iv
    var encryptedData = encodeURIComponent(e.detail.encryptedData)
    if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
      wx.navigateTo({
        url: '/pages/chat/chat',
      })
    } else {
      wx.request({
        url: app.siteInfo.siteroot + 'getPhoneNumber',
        method: "get",
        data: {
          encryptedData: encryptedData,
          iv: iv,
          sessionkey: app.siteInfo.sessionkey,
          uid: app.siteInfo.uid,
          pid: app.siteInfo.pid
        },
        success: function (res) {
          // console.log(res);
          wx.navigateTo({
            url: '/pages/chat/chat',
          })
          that.setData({
            hasphone: false,
          })
        }
      })
    }
  },
  // tabbar跳转
  tabbarNav: function (e) {
    var url = e.currentTarget.dataset.url;
    wx.redirectTo({
      url: url,
    })
  },
  onLoad: function (options) {
    var that = this;
    app.httpRequest('goods/lists', {}, 'get', false, function(res){
      that.setData({
        enterprise: res.data.enterprise,
        category: res.data.category,
        product: res.data.product,
        pageShow: true,
        topbg: res.data.topbg,
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示 
   */
  onShow: function () {
    var that = this;
    var startTime = new Date().getTime();
    that.setData({
      startTime: startTime,
      windowHeight: wx.getStorageSync('systemInfo').windowHeight
    })
  },
  // 获得停留时间，毫秒级
  getStopTime: function () {
    var that = this;
    var endTime = new Date().getTime();
    var startTime = that.data.startTime;
    var stopTime = endTime - startTime;
    that.setData({
      stopTime: stopTime
    })
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var that = this;
    that.getStopTime();
    var stoptime =  that.data.stopTime;
    var percent = that.data.readPercent;
    app.userBehavior(112,'查看了产品列表',stoptime,percent)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this;
    that.getStopTime();
    var stoptime =  that.data.stopTime;
    var percent = that.data.readPercent;
    app.userBehavior(112,'查看了产品列表',stoptime,percent)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    app.userBehavior(113,'分享产品列表')
    return {
      path: "/pages/product/product?share_card_id=" + wx.getStorageSync('share_card_id') + "&share_user_id=" + wx.getStorageSync('user_id'),
      success: function (a) {
      },
      fail: function (a) { }
    };
  }
})